<?php

namespace plugin\admin\app\controller;

use plugin\admin\app\common\Util;
use plugin\admin\app\constant\v1\CommonConstant;
use support\Request;
use support\Response;
use plugin\admin\app\model\V1OrderNodeError;
use plugin\admin\app\controller\Crud;
use support\exception\BusinessException;

/**
 * 强制更换IP管理
 */
class V1OrderNodeErrorController extends Crud
{
    /**
     * 无需登录及鉴权的方法
     * @var array
     */
    protected $noNeedLogin = [];

    /**
     * 需要登录无需权的方法
     * @var array
     */
    protected $noNeedAuth = [
        'select'
    ];

    /**
     * @var V1OrderNodeError
     */
    protected $model = null;

    /**
     * 构造函数
     * @return void
     */
    public function __construct()
    {
        $this->model = new V1OrderNodeError;
    }

    /**
     * 浏览
     * @return Response
     */
    public function index(): Response
    {
        return view('v1-order-node-error/index');
    }

    /**
     * 查询
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function select(Request $request): Response
    {
        [$where, $format, $limit, $field, $order] = $this->selectInput($request);
        $query = $this->doSelect($where, $field, $order);
        return $this->doFormat($query, $format, $limit);
    }

    /**
     * 插入
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function insert(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::insert($request);
        }
        return view('v1-order-node-error/insert');
    }

    /**
     * 更新
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function update(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::update($request);
        }
        return view('v1-order-node-error/update');
    }

    /**
     * 处理操作
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function status(Request $request): Response
    {
        if ($request->method() === 'POST') {
            $primary_key = $this->model->getKeyName();
            $ids = (array)$request->post($primary_key, []);
            if (!$ids) {
                return $this->json(1, '节点参数错误！');
            }
            $list = $this->model->whereIn($primary_key, $ids)->get();
            if ($list->isEmpty()) {
                throw new BusinessException('节点不存在或已删除！');
            }
            $status_1 = $list->where('status', CommonConstant::DEFAULT_1);
            if ($status_1->isNotEmpty()) {
                return $this->json(1, '存在已处理节点！');
            }

            // TODO 2025
            return $this->json(1, '维护中！');

            $ids = [];
            $error = [];
            foreach ($list as $item) {
                $url = 'http://' . $item->ip . ':' . $item->port . '/user/';
                $apiData = [];
                $apiData['key'] = $item->key;
                $apiData['username'] = $item->username;
                $result = Util::userApi('del', $url, $apiData);
                if ($result['code'] == 200) {
                    $ids[] = $item->id;
                } else {
                    $error[] = $item->username;
                }
            }

            $code = 1;
            $msg = '';
            if ($ids) {
                $code = 0;
                $this->model->whereIn($primary_key, $ids)->update(['status' => CommonConstant::DEFAULT_1]);
                $msg .= '处理成功节点' . count($ids) . '条。';
            }
            if ($error) {
                $msg .= '处理失败节点' . implode(',', $error) . '，' . count($error) . '条。';
            }
            return $this->json($code, $msg);
        }
    }

}
